package br.project.cdd.presentation;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.project.cdd.bean.UserBean;
import br.project.cdd.vo.UserVO;

public class UserServlet extends HttpServlet {
	
	@Override
	public void service(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		
		String user = request.getParameter("txtUser");
		String senha = request.getParameter("txtSenha");
		String confirm_senha = request.getParameter("txtConfirmSenha");	
		int perfil = Integer.parseInt(request.getParameter("ddlPerfil"));
		
		if (user == "" || senha == "" || confirm_senha == "" || (! senha.equals(confirm_senha))){
			HttpSession session = request.getSession();
			session.setAttribute("val", "0");
			session.setAttribute("user", user);
			response.sendRedirect("index.jsp");
		}
		else{
			HttpSession session = request.getSession();
			if (session.getAttribute("user") != null)
				session.removeAttribute("user");
			if (session.getAttribute("val") != null)
				session.removeAttribute("val");
			
			inserirUsuario(user, senha, perfil);
			response.sendRedirect("index.jsp");			
		}		
	}
	
	private void inserirUsuario(String user, String senha, int perfil){
		UserBean usr = new UserBean();
		usr.setNmUsuario(user);
		usr.setSenha(senha);
		
		UserVO us = new UserVO();
		us.inserirUsuario(usr, perfil);
	}
	
}
